define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            $(".btn-add").data("area", ["90%", "80%"]);
            $(".btn-edit").data("area", ["90%", "80%"]);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'card/kabaw/index' + location.search,
                    add_url: 'card/kabaw/add',
                    edit_url: 'card/kabaw/edit',
                    del_url: 'card/kabaw/del',
                    multi_url: 'card/kabaw/multi',
                    import_url: 'card/kabaw/import',
                    table: 'card_kabaw',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                visible: false,
                showToggle: false,
                showColumns: false,
                search:false,
                showExport: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'illustrate', title: __('Illustrate'), operate:false},
                        /*{field: 'lifespan_data', title: __('Lifespan_data'),formatter: function (row,index) {
                                var str=index.lifespan_data_text+" "+index.fate;
                                if(index.lifespan_data==0){
                                    str=index.lifespan_data_text+" "+index.start_time_text+"——"+index.end_time_text;
                                }
                                return str;
                            }
                        },*/
                       /* {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime,visible: false},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime,visible: false},
                        */{field: 'fate', title: __('Fate')},
                        {field: 'online_switch', title: __('Online_switch'),searchList: {"0":__('Online_switch 0'),"1":__('Online_switch 1')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'all_cost', title: __('All_cost'), operate:false},
                        {field: 'all_price', title: __('All_price'), operate:false},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table,
                            buttons: [
                                {
                                    name: 'see_kabawtype',
                                    title: __('查看明细'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    text: '查看明细',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='card/kabawtype/index?card_kabaw_id='+row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'see',
                                    title: __('查看'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    text: '查看',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='card/kabaw/see?id='+row.id;
                                        return url;
                                    },
                                },
                            ],
                            events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            table.on('post-body.bs.table',function () {
                $(".btn-editone").data("area", ["90%", "80%"]);
            })
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {

            Controller.api.bindevent();
            Template.helper("Fast", Fast);

            $(document).on("fa.event.appendfieldlist", "#second-table .btn-append", function (e, obj) {
                //绑定动态下拉组件
                console.log(obj)
                Form.events.selectpage(obj);
            });

        },
        edit: function () {
            Controller.api.bindevent();
            Template.helper("Fast", Fast);

            $(document).on("fa.event.appendfieldlist", "#second-table .btn-append", function (e, obj) {
                //绑定动态下拉组件
                Form.events.selectpage(obj);
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
$(document).on("change", ".number_class", function(){
    //后续操作
    var index=$(this).attr("data-id");
    var price=$("input[name='row[event]["+index+"][price]']").val();
    var number=$(this).val();
    if(price>0){
        var all_cost=parseFloat(price)*Number(number);
        all_cost=keepDecimal(all_cost);
    }
    console.log(price)
    if(all_cost==undefined) all_cost=0;
    var data=JSON.parse($(".all_data").val());
    data[index]["price"]=price;
    var cost=all_cost_js(data);
    $(".all_cost").html("核算总成本："+cost);
    $("#all_cost_"+index).html(all_cost);
});
$(document).on("change", ".price_class", function(){
    //后续操作
    var index=$(this).attr("data-id");
    var number=$("input[name='row[event]["+index+"][number]']").val();
    var price=$(this).val();
    if(number>0){
        var all_cost=parseFloat(price)*Number(number);
        all_cost=keepDecimal(all_cost);
    }
    if(all_cost==undefined) all_cost=0;
    var data=JSON.parse($(".all_data").val());
    var cost=all_cost_js(data);
    $(".all_cost").html("核算总成本："+cost);
    $("#all_cost_"+index).html(all_cost);
});
function keepDecimal(num) {
    var result = parseFloat(num);
    if (isNaN(result)) {
        return 0;
    }
    result = Math.round(num * 100) / 100;
    return result;
}

function accAdd(arg1,arg2){
    var r1,r2,m;
    try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
    try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
    m=Math.pow(10,Math.max(r1,r2))
    return (arg1*m+arg2*m)/m
}
function all_cost_js(data,type=0){
    var cost=0;
    $.each(data, function (i, j) {
        if(type==1){
            if(cost.toString().indexOf(".") == -1){
                cost+=".00";
            }
            if(j.all_price.toString().indexOf(".") == -1){
                j.all_price+=".00";
            }
            cost=accAdd(cost,j.all_price)
        }else{
            var cost1=parseFloat(j.price)*Number(j.number);
            console.log(j,11)
            cost+=keepDecimal(cost1);
        }
    });
    return cost;
}
//总价格
$(document).on("change", ".all_price_class", function(){
    //后续操作
    var data=JSON.parse($(".all_data").val());
    var cost=all_cost_js(data,1);
    $(".all_price").html("总价格："+cost);
});
//删除按钮
$(document).on("click", ".btn-remove", function(){
    var data=JSON.parse($(".all_data").val());
    var cost=all_cost_js(data,1);
    $(".all_price").html("总价格："+cost);
    var cost=all_cost_js(data);
    $(".all_cost").html("核算总成本："+cost);
});

/*2023-5-16 修改逻辑*/
$(document).on("change", ".card_type", function(){
    //后续操作
    var index=$(this).attr("data-id");
    var cost=$("select[name='row[event]["+index+"][card_type_id]'] :checked").attr("data-price");
    $("input[name='row[event]["+index+"][price]']").val(cost);

    var data=JSON.parse($(".all_data").val());
    data[index]["price"]=cost;
    var number=$("input[name='row[event]["+index+"][number]']").val();
    if(number>0){
        var all_cost=parseFloat(cost)*Number(number);
        all_cost=keepDecimal(all_cost);
    }
    if(all_cost==undefined) all_cost=0;
    var cost=all_cost_js(data);
    console.log(cost);
    $(".all_cost").html("核算总成本："+cost);
    $("#all_cost_"+index).html(all_cost);
});

